//
//  DYUtility.m
//  DaYing
//
//  Created by DaYing  on 2023.
//  Copyright © 2023 DaYing . All rights reserved.
//


#import "DYUtility.h"

@implementation DYUtility

#pragma mark - 时间格式化

+ (NSString *)stringFromDateTimeToMinutes:(int64_t)time formart:(NSString *)formart
{
    NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"GMT+8:00"];
    [NSTimeZone setDefaultTimeZone:tzGMT];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:tzGMT];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息 +0000。
    
    [dateFormatter setDateFormat:formart];
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
}

#pragma mark - 播放时间格式化

+ (NSString *)getStringFromTime:(float)currentTimeValue {
    NSInteger componentshour = (NSInteger)currentTimeValue / 3600;
    NSInteger componentsminute = (NSInteger)currentTimeValue % 3600 / 60;
    NSInteger componentssecond = (NSInteger)currentTimeValue % 3600 % 60;
    
    NSString *hour = @"00";
    if (componentshour > 9) {
        hour = [NSString stringWithFormat:@"%ld",(long)componentshour];
    } else {
        hour = [NSString stringWithFormat:@"0%ld",(long)componentshour];
    }
    NSString *minute = @"00";
    if (componentsminute > 9) {
        minute = [NSString stringWithFormat:@"%ld",(long)componentsminute];
    } else {
        minute = [NSString stringWithFormat:@"0%ld",(long)componentsminute];
    }
    NSString *second = @"00";
    if (componentssecond > 9) {
        second = [NSString stringWithFormat:@"%ld",(long)componentssecond];
    } else {
        second = [NSString stringWithFormat:@"0%ld",(long)componentssecond];
    }
    
    if (componentshour > 0) {
        return [NSString stringWithFormat:@"%@:%@:%@",hour,minute,second];
    } else {
        return [NSString stringWithFormat:@"%@:%@",minute,second];
    }
}

#pragma mark 数据处理

//正则去除标签
+ (NSString *)removeHtmlWithString:(NSString *)htmlString {
    //替换某个字符
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];
    NSRegularExpression * regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n" options:0 error:nil];
    htmlString = [regularExpretion stringByReplacingMatchesInString:htmlString options:NSMatchingReportProgress range:NSMakeRange(0, htmlString.length) withTemplate:@""];
     return htmlString;
}

#pragma mark 样式

// 设置文字中关键字背景颜色
+ (NSMutableAttributedString *)searchTitle:(NSString *)title key:(NSString *)key keyColor:(UIColor *)keyColor {
    
    NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc] initWithString:kSafeString(title)];
    NSString *copyStr = kSafeString(title);
    
    NSMutableString *xxstr = [NSMutableString new];
    for (int i = 0; i < key.length; i++) {
        [xxstr appendString:@"*"];
    }
    
    while ([copyStr rangeOfString:key options:NSCaseInsensitiveSearch].location != NSNotFound) {
        
        NSRange range = [copyStr rangeOfString:key options:NSCaseInsensitiveSearch];
        
        [titleStr addAttribute:NSBackgroundColorAttributeName value:keyColor range:range];
        copyStr = [copyStr stringByReplacingCharactersInRange:NSMakeRange(range.location, range.length) withString:xxstr];
    }
    return titleStr;
}

+ (NSMutableAttributedString *)getNewColorStr:(NSString *)headStr headFont:(UIFont *)headFont headColor:(UIColor *)headColor lastStr:(NSString *)lastStr lastFont:(UIFont *)lastFont lastColor:(UIColor *)lastColor
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",headStr,lastStr]];
    [str addAttribute:NSForegroundColorAttributeName value:headColor range:NSMakeRange(0,headStr.length)];
    [str addAttribute:NSForegroundColorAttributeName value:lastColor range:NSMakeRange(headStr.length, lastStr.length)];
    [str addAttribute:NSFontAttributeName value:headFont range:NSMakeRange(0,headStr.length)];
    [str addAttribute:NSFontAttributeName value:lastFont range:NSMakeRange(headStr.length, lastStr.length)];
    
    return str;
}

+ (NSMutableAttributedString *)getNewColorStr:(NSString *)headStr headFont:(UIFont *)headFont headColor:(UIColor *)headColor middleStr:(NSString *)middleStr middleFont:(UIFont *)middleFont middleColor:(UIColor *)middleColor footStr:(NSString *)footStr footFont:(UIFont *)footFont footColor:(UIColor *)footColor {
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",headStr,middleStr,footStr]];
    [str addAttribute:NSForegroundColorAttributeName value:headColor range:NSMakeRange(0,headStr.length)];
    [str addAttribute:NSForegroundColorAttributeName value:middleColor range:NSMakeRange(headStr.length, middleStr.length)];
    [str addAttribute:NSForegroundColorAttributeName value:footColor range:NSMakeRange(headStr.length+middleStr.length, footStr.length)];
    
    [str addAttribute:NSFontAttributeName value:headFont range:NSMakeRange(0,headStr.length)];
    [str addAttribute:NSFontAttributeName value:middleFont range:NSMakeRange(headStr.length, middleStr.length)];
    [str addAttribute:NSFontAttributeName value:footFont range:NSMakeRange(headStr.length+middleStr.length, footStr.length)];
    return str;
}

+ (NSMutableAttributedString *)getNewColorWithAry:(NSArray *)ary {
//    @[@{@"content":@"已接受您的邀请加入您的图谱《",
//    @"font":[UIFont systemFontOfSize:14],
//    @"color":UIColorFromHex(0x333333)}]
      
    __block NSString *str = @"";
    [ary enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *content = dic[@"content"];
        str = [str stringByAppendingString:content];
    }];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
    
    __block NSInteger length = 0;
    [ary enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *content = dic[@"content"];
        UIFont *font = dic[@"font"];
        UIColor *color = dic[@"color"];

        [attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(length,content.length)];
        [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(length,content.length)];
        
        length += content.length;
    }];
    return attributedString;
}

//数字字符串校验
+ (BOOL)numVerification:(NSString *)num {
    if (num.length > 0) {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
        NSString *filtered = [[num componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [num isEqualToString:filtered];
        if(!basicTest) {
            //只能输入数字
            return NO;
        }
        
        if ([[num substringToIndex:1] isEqualToString:@"0"] && num.length >= 2) {
            return NO;
        }
        
        //没有小数点限制输入8位
        if (num.length > 8) {
            return NO;
        } else {
          return YES;
        }
    } else {
        return YES;
    }
}

+ (NSString*)dictionaryToJson:(NSDictionary *)dic {
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

@end
